const path = require('path');
const conf = require('./conf')

const config = {
  projectName: 'taro-framework',
  date: '2019-11-2',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  sourceRoot: 'src',
  outputRoot: 'h5',
  alias: {
    '@': path.resolve(__dirname, '../', 'src')
  },
  // babel、csso、uglify 等配置从 plugins 配置中移出来
  babel: {
    sourceMap: true,
    presets: [['env', { modules: false }]],
    plugins: [
      'transform-decorators-legacy',
      'transform-class-properties',
      'transform-object-rest-spread',
      ['transform-runtime', {
        "helpers": false,
        "polyfill": false,
        "regenerator": true,
        "moduleName": 'babel-runtime'
      }]
    ]
  },
  "compact":false,
  // 小程序配置从 weapp 改为 mini，可以删掉很多小配置
  mini: {
    webpackChain(chain, webpack) {
      chain.merge({
        optimization: {
          splitChunks: {
            cacheGroups: {
              lodash: {
                name: 'lodash',
                priority: 1000,
                test(module) {
                  return /node_modules[\\/]lodash/.test(module.context)
                }
              }
            }
          }
        }
      })
      chain.plugin('analyzer')
        .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [])
    },
    cssLoaderOption: {},
    commonChunks(commonChunks) {
      commonChunks.push('lodash')
      return commonChunks
    },
    postcss: {
      pxtransform: {
        enable: true,
        config: {}
      },
      url: {
        enable: true,
        config: {
          limit: 10240 // 设定转换尺寸上限
        }
      }
    }
  },
  copy: {
    patterns: [
      // 需添加如下配置
      {
        from: 'node_modules/taro-echarts/components/ec-canvas/',
        to: 'dist/npm/taro-echarts/components/ec-canvas',
        ignore: ['ec-canvas.js']
      }
    ],
    options: {
    }
  },
  // 可以删掉很多小配置
  h5: {
    publicPath: conf.isDev ? '/' : '/h5/',
    staticDirectory: 'static',
    esnextModules: ['taro-echarts'],
    webpackChain(chain, webpack) { },
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
          browsers: [
            'last 3 versions',
            'Android >= 4.1',
            'ios >= 8'
          ]
        }
      }
    }
  }
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}
